﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.App_Code;
using QXBook.Utils;
using QXBook.Model;
using QXBook.Service;

namespace Web.SysManage.Admin
{
    public partial class AdminRoleEdit : AdminBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(CommonUtils.Get("RoleID")))
                {
                    Title = "修改角色组";
                    lblTitle.Text = "修改角色组";
                    hRoleID.Value = CommonUtils.Get("RoleID");
                    MembersInfo member = ServiceFactory.CreateInstance<AdminService>().GetModel(hRoleID.Value);
                    this.DataBind(member);
                }
                else
                {
                    Title = "添加角色组";
                    lblTitle.Text = "添加角色组";
                }
            }
            //else
            //{
            //    this.AjaxMethod();
            //}

            btnSave.Click += new EventHandler(btnSave_Click);
        }

        void btnSave_Click(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            Save();
        }

        private void DataBind(IModelBase model)
        {
            MembersRoleInfo role = model as MembersRoleInfo;
            iRoleName.Value = role.RoleName;
            taRemark.Value = role.RoleName;
            this.hpopdom.Value = role.PopDomStr;
        }

        private MembersRoleInfo GetModel(ref MembersRoleInfo model)
        {
            string roleName = CommonUtils.Post("iRoleName");
            string content = CommonUtils.Post("taRemark");
            string popdom = CommonUtils.Post("hpopdom");

            model.RoleName = roleName;
            model.PopDomStr = popdom;
            return model;
        }

        private void Save()
        {
            string rtmessage = string.Empty;
            string resultMsg = string.Empty;
            try
            {
                MembersRoleInfo member = string.IsNullOrEmpty(hRoleID.Value) ? new MembersRoleInfo() : ServiceFactory.CreateInstance<AdminRuleService>().GetAdminRoleInfoByRoleId(CommonUtils.ToInt(hRoleID.Value));
                member = GetModel(ref member);

                if (!string.IsNullOrEmpty(hRoleID.Value))
                {
                    rtmessage = ServiceFactory.CreateInstance<AdminRuleService>().UpdateAdminRole(base.adminInfo, member);
                    resultMsg = "角色组信息修改成功!";
                }
                else
                {
                    rtmessage = ServiceFactory.CreateInstance<AdminRuleService>().AddAdminRole(base.adminInfo, member);
                    try
                    {
                        CachingService.Remove(CachingService.CACHING_ALL_ADMIN_ENTITY);
                    }
                    catch (Exception ex)
                    {
                        base.ajaxdata = "{state:false,message:\"" + CommonUtils.JSEncode(ex.Message.ToString()) + "\"}";
                        base.AjaxErch(base.ajaxdata);
                        return;
                    }

                    resultMsg = "成功添加新的角色组!";
                }
            }
            catch (Exception ex)
            {
                base.ResponseErch("{state:false,message:\"" + CommonUtils.JSEncode(ex.Message.ToString()) + "\"}");
                return;
            }

            base.ResponseErch(rtmessage, resultMsg);
        }
    }
}